package com.example.springbootredis.controller;


import com.example.springbootredis.entiy.Classroom;
import com.example.springbootredis.service.ClassroomService;
import com.example.springbootredis.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("classroom")
@CrossOrigin(maxAge = 3600)
public class ClassroomController {

    @Autowired
    private ClassroomService classroomService;

    // 新增教室
    @PostMapping("save")
    public JsonResult save(@RequestBody Classroom classroom) {
        // 必填验证
        if (classroom.getClassroomNo() == null || classroom.getClassroomNo().isEmpty()) {
            return JsonResult.failed("教室编号不能为空");
        }
        if (classroom.getCampus() == null || classroom.getCampus().isEmpty()) {
            return JsonResult.failed("校区不能为空");
        }
        if (classroom.getClassroomName() == null || classroom.getClassroomName().isEmpty()) {
            return JsonResult.failed("教室名称不能为空");
        }
        if (classroom.getClassroomAddress() == null || classroom.getClassroomAddress().isEmpty()) {
            return JsonResult.failed("教室地址不能为空");
        }

        // 检查教室编号是否重复
        if (classroom.getId() == null) {
            // 新增时检查
            if (classroomService.existsByClassroomNo(classroom.getClassroomNo())) {
                return JsonResult.failed("教室编号已存在");
            }
        } else {
            // 修改时检查
            if (classroomService.existsByClassroomNoAndIdNot(classroom.getClassroomNo(), classroom.getId())) {
                return JsonResult.failed("教室编号已存在");
            }
        }

        // 检查教室名称是否重复
        if (classroom.getId() == null) {
            // 新增时检查
            if (classroomService.existsByClassroomName(classroom.getClassroomName())) {
                return JsonResult.failed("教室名称已存在");
            }
        } else {
            // 修改时检查
            if (classroomService.existsByClassroomNameAndIdNot(classroom.getClassroomName(), classroom.getId())) {
                return JsonResult.failed("教室名称已存在");
            }
        }

        Classroom res = classroomService.save(classroom);
        if (res == null) {
            return JsonResult.failed("操作失败");
        }

        return JsonResult.success("操作成功", res);
    }

    // 教室列表
    @GetMapping("list")
    public JsonResult list() {
        List<Classroom> classroomList = classroomService.findAll();
        return JsonResult.success("获取成功", classroomList);
    }

    // 根据校区筛选教室
    @GetMapping("listByCampus")
    public JsonResult listByCampus(@RequestParam("campus") String campus) {
        List<Classroom> classroomList = classroomService.findByCampus(campus);
        return JsonResult.success("获取成功", classroomList);
    }

    // 根据教室名关键字模糊查询
    @GetMapping("searchByName")
    public JsonResult searchByName(@RequestParam("classroomName") String classroomName) {
        List<Classroom> classroomList = classroomService.findByClassroomNameContaining(classroomName);
        return JsonResult.success("获取成功", classroomList);
    }

    // 根据校区和教室名联合查询
    @GetMapping("search")
    public JsonResult search(@RequestParam(value = "campus", required = false) String campus,
                             @RequestParam(value = "classroomName", required = false) String classroomName) {
        List<Classroom> classroomList;
        if (campus != null && !campus.isEmpty() && classroomName != null && !classroomName.isEmpty()) {
            classroomList = classroomService.findByCampusAndClassroomNameContaining(campus, classroomName);
        } else if (campus != null && !campus.isEmpty()) {
            classroomList = classroomService.findByCampus(campus);
        } else if (classroomName != null && !classroomName.isEmpty()) {
            classroomList = classroomService.findByClassroomNameContaining(classroomName);
        } else {
            classroomList = classroomService.findAll();
        }
        return JsonResult.success("获取成功", classroomList);
    }

    // 根据教室id查询单个教室详情
    @GetMapping("detail")
    public JsonResult detail(@RequestParam("id") Long id) {
        Optional<Classroom> classroom = classroomService.findById(id);
        if (classroom.isPresent()) {
            return JsonResult.success("获取成功", classroom.get());
        } else {
            return JsonResult.failed("教室不存在");
        }
    }

    // 删除教室
    @GetMapping("delete")
    public JsonResult delete(@RequestParam("id") Long id) {
        classroomService.deleteById(id);
        return JsonResult.success("删除成功", null);
    }
}